{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/4.0/\"><img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https://i.creativecommons.org/l/by-sa/4.0/88x31.png\" /></a><br />This work is licensed under a <a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/4.0/\">Creative Commons Attribution-ShareAlike 4.0 International License</a>.\n",
    "\n",
    "\n",
    "\n",
    "<div class=\"jumbotron text-center\">\n",
    "<img src=\"https://www.utadeo.edu.co/sites/tadeo/files/styles/brand_250x250/public/node/brand/images/field_brand_image/elementos_0.jpg?itok=-Aiq-SvW\" style=\"height: 5em; width:auto\" >\n",
    "<img src=\"https://www.juliaopt.org/images/juliaopt.svg\" style=\"height: 5em; width:auto\" src=\"/images/juliaopt.svg\">\n",
    "    <h1>Casos de aplicacion del paquete JuMP</h1>\n",
    "    <p>\n",
    "      Para ver mas del lenguaje de programacion julia : <a href=\"http://julialang.org\">Julia language.</a>\n",
    "    </p>\n",
    "  </div>\n",
    "<img src=\"https://i.gifer.com/JKkS.gif\" style=\"height: 10em; width:auto\" src=\"/images/juliaopt.svg\">\n",
    "**Autor**: Jorge Ivan Romero\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Primer Caso Embarque de madera\n",
    "\n",
    "### Uso de matrices y sumaproductos \n",
    "\n",
    " \n",
    "$\\sum_{i=i}^{n}\\sum_{j=i}^{n}$ $X_{ij}$ $*$ $C_{ij}$\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Captura.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Captura.PNG?raw=true)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Min 58.5 x[1,1] + 65.3 x[2,1] + 59 x[3,1] + 68.3 x[1,2] + 74.8 x[2,2] + 61.3 x[3,2] + 47.8 x[1,3] + 55 x[2,3] + 63.5 x[3,3] + 55 x[1,4] + 49 x[2,4] + 58.8 x[3,4] + 63.5 x[1,5] + 57.5 x[2,5] + 50 x[3,5]\n",
      "Subject to\n",
      " x[1,1] + x[1,2] + x[1,3] + x[1,4] + x[1,5] = 15\n",
      " x[2,1] + x[2,2] + x[2,3] + x[2,4] + x[2,5] = 20\n",
      " x[3,1] + x[3,2] + x[3,3] + x[3,4] + x[3,5] = 15\n",
      " x[1,1] + x[2,1] + x[3,1] = 11\n",
      " x[1,2] + x[2,2] + x[3,2] = 12\n",
      " x[1,3] + x[2,3] + x[3,3] = 9\n",
      " x[1,4] + x[2,4] + x[3,4] = 10\n",
      " x[1,5] + x[2,5] + x[3,5] = 8\n",
      " x[i,j] ≥ 0 ∀ i ∈ {1,2,3}, j ∈ {1,2,3,4,5}\n",
      "el minimo costo es=2770.8\n",
      "las asignaciones son=3×5 DataFrames.DataFrame\n",
      "│ Row │ x1  │ x2   │ x3  │ x4   │ x5  │\n",
      "├─────┼─────┼──────┼─────┼──────┼─────┤\n",
      "│ 1   │ 6.0 │ 0.0  │ 9.0 │ 0.0  │ 0.0 │\n",
      "│ 2   │ 5.0 │ 0.0  │ 0.0 │ 10.0 │ 5.0 │\n",
      "│ 3   │ 0.0 │ 12.0 │ 0.0 │ 0.0  │ 3.0 │\n"
     ]
    }
   ],
   "source": [
    "using JuMP,Cbc, DataFrames\n",
    "\n",
    "madera=Model(solver=CbcSolver())\n",
    "\n",
    "costo=[58.5 68.3 47.8 55 63.5;\n",
    "       65.3 74.8 55 49 57.5;\n",
    "       59 61.3 63.5 58.8 50]\n",
    "fuentes=[15;\n",
    "         20;\n",
    "         15]\n",
    "mercados=[11 12 9 10 8]\n",
    "\n",
    "@variable(madera,x[1:3,1:5]>=0)\n",
    "@objective(madera,Min,sum(costo.*x))\n",
    "\n",
    "for i=1:3\n",
    "@constraint(madera,sum(x[i,j] for j=1:5)==fuentes[i])\n",
    "end\n",
    "for j=1:5\n",
    "@constraint(madera, sum(x[i,j] for i=1:3)==mercados[j])\n",
    "end\n",
    "print(madera)\n",
    "status=solve(madera)\n",
    "println(\"el minimo costo es=\",getobjectivevalue(madera))\n",
    "println(\"las asignaciones son=\",DataFrame(getvalue(x)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Min 61 x[1,1] + 69 x[2,1] + 59 x[3,1] + 72 x[1,2] + 78 x[2,2] + 66 x[3,2] + 45 x[1,3] + 60 x[2,3] + 63 x[3,3] + 55 x[1,4] + 49 x[2,4] + 61 x[3,4] + 66 x[1,5] + 56 x[2,5] + 47 x[3,5] + 58.5 x[1,6] + 65.3 x[2,6] + 59 x[3,6] + 68.3 x[1,7] + 74.8 x[2,7] + 61.3 x[3,7] + 47.8 x[1,8] + 55 x[2,8] + 63.5 x[3,8] + 55 x[1,9] + 49 x[2,9] + 58.8 x[3,9] + 63.5 x[1,10] + 57.5 x[2,10] + 50 x[3,10]\n",
      "Subject to\n",
      " x[1,1] + x[1,2] + x[1,3] + x[1,4] + x[1,5] + x[1,6] + x[1,7] + x[1,8] + x[1,9] + x[1,10] = 15\n",
      " x[2,1] + x[2,2] + x[2,3] + x[2,4] + x[2,5] + x[2,6] + x[2,7] + x[2,8] + x[2,9] + x[2,10] = 20\n",
      " x[3,1] + x[3,2] + x[3,3] + x[3,4] + x[3,5] + x[3,6] + x[3,7] + x[3,8] + x[3,9] + x[3,10] = 15\n",
      " x[1,1] + x[1,6] + x[2,1] + x[2,6] + x[3,1] + x[3,6] = 11\n",
      " x[1,2] + x[1,7] + x[2,2] + x[2,7] + x[3,2] + x[3,7] = 12\n",
      " x[1,3] + x[1,8] + x[2,3] + x[2,8] + x[3,3] + x[3,8] = 9\n",
      " x[1,4] + x[1,9] + x[2,4] + x[2,9] + x[3,4] + x[3,9] = 10\n",
      " x[1,5] + x[1,10] + x[2,5] + x[2,10] + x[3,5] + x[3,10] = 8\n",
      " x[i,j] ≥ 0 ∀ i ∈ {1,2,3}, j ∈ {1,2,…,9,10}\n",
      "el minimo costo es=2729.1\n",
      "las asignaciones son=3×10 DataFrames.DataFrame\n",
      "│ Row │ x1  │ x2  │ x3  │ x4   │ x5  │ x6  │ x7   │ x8  │ x9  │ x10 │\n",
      "├─────┼─────┼─────┼─────┼──────┼─────┼─────┼──────┼─────┼─────┼─────┤\n",
      "│ 1   │ 0.0 │ 0.0 │ 9.0 │ 0.0  │ 0.0 │ 6.0 │ 0.0  │ 0.0 │ 0.0 │ 0.0 │\n",
      "│ 2   │ 0.0 │ 0.0 │ 0.0 │ 10.0 │ 5.0 │ 5.0 │ 0.0  │ 0.0 │ 0.0 │ 0.0 │\n",
      "│ 3   │ 0.0 │ 0.0 │ 0.0 │ 0.0  │ 3.0 │ 0.0 │ 12.0 │ 0.0 │ 0.0 │ 0.0 │\n"
     ]
    }
   ],
   "source": [
    "using JuMP,Cbc, DataFrames\n",
    "\n",
    "madera=Model(solver=CbcSolver())\n",
    "costo=[61 72 45 55 66 58.5 68.3 47.8 55 63.5;\n",
    "       69 78 60 49 56 65.3 74.8 55 49 57.5;\n",
    "       59 66 63 61 47 59 61.3 63.5 58.8 50]\n",
    "fuentes=[15;\n",
    "         20;\n",
    "         15]\n",
    "mercados=[11 12 9 10 8]\n",
    "\n",
    "@variable(madera,x[1:3,1:10]>=0)\n",
    "@objective(madera,Min,sum(costo.*x))\n",
    "\n",
    "for i=1:3\n",
    "@constraint(madera,sum(x[i,j] for j=1:10)==fuentes[i])\n",
    "end\n",
    "            \n",
    "for j=1:5\n",
    "@constraint(madera, sum(x[i,j]+x[i,j+5] for i=1:3)==mercados[j])\n",
    "end\n",
    "print(madera)\n",
    "status=solve(madera)\n",
    "println(\"el minimo costo es=\",getobjectivevalue(madera))\n",
    "println(\"las asignaciones son=\",DataFrame(getvalue(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Segundo caso Preocupacion por la capacidad"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso2-1.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso2-1.PNG?raw=true)\n",
    "\n",
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso2-2.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso2-2.PNG?raw=true)\n",
    "\n",
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso2-3.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso2-3.PNG?raw=true)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Min 2500 x[1,1] + 5000 x[2,1] + 9000 x[3,1] + 18750 x[4,1] + 2500 x[1,2] + 5000 x[2,2] + 9000 x[3,2] + 18750 x[4,2] + 2500 x[1,3] + 5000 x[2,3] + 10000 x[3,3] + 25000 x[4,3] + 2500 x[1,4] + 5000 x[2,4] + 10000 x[3,4] + 25000 x[4,4] + 2500 x[1,5] + 5000 x[2,5] + 10000 x[3,5] + 25000 x[4,5]\n",
      "Subject to\n",
      " 30 x[1,1] + 80 x[2,1] + 200 x[3,1] + 2000 x[4,1] + 30 x[1,2] + 80 x[2,2] + 200 x[3,2] + 2000 x[4,2] ≥ 60\n",
      " 30 x[1,1] + 80 x[2,1] + 200 x[3,1] + 2000 x[4,1] + 30 x[1,2] + 80 x[2,2] + 200 x[3,2] + 2000 x[4,2] + 30 x[1,3] + 80 x[2,3] + 200 x[3,3] + 2000 x[4,3] ≥ 260\n",
      " 30 x[1,1] + 80 x[2,1] + 200 x[3,1] + 2000 x[4,1] + 30 x[1,2] + 80 x[2,2] + 200 x[3,2] + 2000 x[4,2] + 30 x[1,3] + 80 x[2,3] + 200 x[3,3] + 2000 x[4,3] + 30 x[1,4] + 80 x[2,4] + 200 x[3,4] + 2000 x[4,4] ≥ 290\n",
      " 30 x[1,1] + 80 x[2,1] + 200 x[3,1] + 2000 x[4,1] + 30 x[1,2] + 80 x[2,2] + 200 x[3,2] + 2000 x[4,2] + 30 x[1,3] + 80 x[2,3] + 200 x[3,3] + 2000 x[4,3] + 30 x[1,4] + 80 x[2,4] + 200 x[3,4] + 2000 x[4,4] + 30 x[1,5] + 80 x[2,5] + 200 x[3,5] + 2000 x[4,5] ≥ 365\n",
      " 2500 x[1,1] + 5000 x[2,1] + 9000 x[3,1] + 18750 x[4,1] + 2500 x[1,2] + 5000 x[2,2] + 9000 x[3,2] + 18750 x[4,2] ≤ 9500\n",
      " x[i,j] ≥ 0, integer, ∀ i ∈ {1,2,3,4}, j ∈ {1,2,3,4,5}\n",
      "el minimo costo es=19000.0\n",
      "las asignaciones son=4×5 DataFrames.DataFrame\n",
      "│ Row │ x1  │ x2  │ x3  │ x4  │ x5  │\n",
      "├─────┼─────┼─────┼─────┼─────┼─────┤\n",
      "│ 1   │ 0.0 │ 0.0 │ 0.0 │ 0.0 │ 0.0 │\n",
      "│ 2   │ 0.0 │ 0.0 │ 0.0 │ 0.0 │ 0.0 │\n",
      "│ 3   │ 1.0 │ 0.0 │ 1.0 │ 0.0 │ 0.0 │\n",
      "│ 4   │ 0.0 │ 0.0 │ 0.0 │ 0.0 │ 0.0 │\n"
     ]
    }
   ],
   "source": [
    "using Cbc,JuMP,DataFrames\n",
    "\n",
    "coeficientes=[2500\t2500\t2500\t2500\t2500;\n",
    "5000\t5000\t5000\t5000\t5000;\n",
    "9000\t9000\t10000\t10000\t10000;\n",
    "18750\t18750\t25000\t25000\t25000]\n",
    "\n",
    "personas=[30\t30\t30\t30\t30;\n",
    "80\t80\t80\t80\t80;\n",
    "200\t200\t200\t200\t200;\n",
    "2000\t2000\t2000\t2000\t2000]\n",
    "\n",
    "servidores=Model(solver=CbcSolver())\n",
    "@variables servidores begin\n",
    "    x[1:4,1:5]>=0, Int\n",
    "end\n",
    "@objective(servidores,Min,sum(x.*coeficientes))\n",
    "\n",
    "@constraints servidores begin\n",
    "sum(personas[1:4,1:2].*x[1:4,1:2])>=60\n",
    "sum(personas[1:4,1:3].*x[1:4,1:3])>=260\n",
    "sum(personas[1:4,1:4].*x[1:4,1:4])>=290\n",
    "sum(personas[1:4,1:5].*x[1:4,1:5])>=365\n",
    "sum(coeficientes[1:4,1:2].*x[1:4,1:2])<=9500\n",
    "\n",
    "end\n",
    "        print(servidores)\n",
    "\n",
    "status=solve(servidores)\n",
    "println(\"el minimo costo es=\",getobjectivevalue(servidores))\n",
    "println(\"las asignaciones son=\",DataFrame(getvalue(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tercer caso"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso3-1.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso3-1.PNG?raw=true)\n",
    "\n",
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso3-2.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso3-2.PNG?raw=true)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Max 110 x1 + 210 x2 + 60.5 x3 + 53.5 x4 + 143.25 x5 + 155.25 x6 + 136 x7 + 66.25 x8 + 33.75 x9 + 22 x10 + 26.62 x11\n",
      "Subject to\n",
      " 3 x1 + 2.5 x6 ≤ 45000\n",
      " 2 x1 + 1.5 x5 + 1.5 x6 + 2 x7 ≤ 28000\n",
      " 1.5 x2 ≤ 9000\n",
      " 1.5 x3 + 0.5 x4 ≤ 18000\n",
      " 2 x5 + 1.5 x11 ≤ 30000\n",
      " 3 x7 + 1.5 x10 ≤ 20000\n",
      " 1.5 x8 + 0.5 x9 ≤ 30000\n",
      " x7 ≤ 5500\n",
      " x10 ≤ 6000\n",
      " x2 ≤ 4000\n",
      " x3 ≤ 12000\n",
      " x4 ≤ 15000\n",
      " x1 ≤ 7000\n",
      " x1 ≥ 4200\n",
      " x6 ≤ 5000\n",
      " x6 ≥ 3000\n",
      " x5 ≥ 2800\n",
      " x1 ≥ 0\n",
      " x2 ≥ 0, integer\n",
      " x3 ≥ 0, integer\n",
      " x4 ≥ 0, integer\n",
      " x5 ≥ 0, integer\n",
      " x6 ≥ 0, integer\n",
      " x7 ≥ 0, integer\n",
      " x8 ≥ 0, integer\n",
      " x9 ≥ 0, integer\n",
      " x10 ≥ 0, integer\n",
      " x11 ≥ 0, integer\n",
      "El mayor valor es = 6.86287176e6\n",
      "x1.pantalones de lana = 4200.0\n",
      "x2.sueter de cashmir = 4000.0\n",
      "x3.blusa de seda = 7000.0\n",
      "x4.camisola de seda = 15000.0\n",
      "x5.falda ajustada = 8064.0\n",
      "x6.chaqueta de lana= 5000.0\n",
      "x7.pantalones de tersiopelo = 2.0\n",
      "x8.sueter de algodon = 0.0\n",
      "x9.minifalda de algodon = 60000.0\n",
      "x10.camisa de terciopelo = 6000.0\n",
      "x11.blusa de botones = 9248.0\n"
     ]
    }
   ],
   "source": [
    "using JuMP, Cbc\n",
    "\n",
    "moda=Model(solver=CbcSolver())\n",
    "@variable(moda,x1>=0)# X1 son pantalones de lana\n",
    "@variable(moda,x2>=0,Int)\n",
    "@variable(moda,x3>=0,Int)\n",
    "@variable(moda,x4>=0,Int)\n",
    "@variable(moda,x5>=0,Int)\n",
    "@variable(moda,x6>=0,Int)\n",
    "@variable(moda,x7>=0,Int)\n",
    "@variable(moda,x8>=0,Int)\n",
    "@variable(moda,x9>=0,Int)\n",
    "@variable(moda,x10>=0,Int)\n",
    "@variable(moda,x11>=0,Int)\n",
    "\n",
    "@objective(moda,Max,110x1+210x2+60.5x3+53.5x4+143.25x5+155.25x6+136x7+66.25x8+33.75x9+22x10+26.62x11)\n",
    "\n",
    "# Restricciones de materia prima\n",
    "\n",
    "@constraint(moda,3x1+2.5x6<=45000)\n",
    "@constraint(moda,2x1+1.5x5+1.5x6+2x7<=28000)\n",
    "@constraint(moda,1.5x2<=9000)\n",
    "@constraint(moda,1.5x3+0.5x4<=18000)\n",
    "@constraint(moda,2x5+1.5x11<=30000)\n",
    "@constraint(moda,3x7+1.5x10<=20000)\n",
    "@constraint(moda,1.5x8+0.5x9<=30000)\n",
    "\n",
    "#Restricciones de demanda\n",
    "@constraint(moda,x7<=5500)\n",
    "@constraint(moda,x10<=6000)\n",
    "@constraint(moda,x2<=4000)\n",
    "@constraint(moda,x3<=12000)\n",
    "@constraint(moda,x4<=15000)\n",
    "@constraint(moda,x1<=7000)\n",
    "@constraint(moda,x1>=4200)\n",
    "@constraint(moda,x6<=5000)\n",
    "@constraint(moda,x6>=3000)\n",
    "@constraint(moda,x5>=2800)\n",
    "\n",
    "print(moda)\n",
    "status=solve(moda)\n",
    "println(\"El mayor valor es = \",getobjectivevalue(moda))\n",
    "println(\"x1.pantalones de lana = \",getvalue(x1))\n",
    "println(\"x2.sueter de cashmir = \",getvalue(x2))\n",
    "println(\"x3.blusa de seda = \",getvalue(x3))\n",
    "println(\"x4.camisola de seda = \",getvalue(x4))\n",
    "println(\"x5.falda ajustada = \",getvalue(x5))\n",
    "println(\"x6.chaqueta de lana= \",getvalue(x6))\n",
    "println(\"x7.pantalones de tersiopelo = \",getvalue(x7))\n",
    "println(\"x8.sueter de algodon = \",getvalue(x8))\n",
    "println(\"x9.minifalda de algodon = \",getvalue(x9))\n",
    "println(\"x10.camisa de terciopelo = \",getvalue(x10))\n",
    "println(\"x11.blusa de botones = \",getvalue(x11))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Max 110 x[1,1] + 210 x[1,2] + 60.5 x[1,3] + 53.5 x[1,4] + 143.25 x[1,5] + 155.25 x[1,6] + 136 x[1,7] + 66.25 x[1,8] + 33.75 x[1,9] + 22 x[1,10] + 26.62 x[1,11]\n",
      "Subject to\n",
      " 3 x[1,1] + 2.5 x[1,6] ≤ 45000\n",
      " 2 x[1,1] + 1.5 x[1,5] + 1.5 x[1,6] + 2 x[1,7] ≤ 28000\n",
      " 1.5 x[1,2] ≤ 9000\n",
      " 1.5 x[1,3] + 0.5 x[1,4] ≤ 18000\n",
      " 2 x[1,5] + 1.5 x[1,11] ≤ 30000\n",
      " 3 x[1,7] + 1.5 x[1,10] ≤ 20000\n",
      " 1.5 x[1,8] + 0.5 x[1,9] ≤ 30000\n",
      " x[1,7] ≤ 5500\n",
      " x[1,10] ≤ 6000\n",
      " x[1,2] ≤ 4000\n",
      " x[1,3] ≤ 12000\n",
      " x[1,4] ≤ 15000\n",
      " x[1,1] ≤ 7000\n",
      " x[1,1] ≥ 4200\n",
      " x[1,6] ≤ 5000\n",
      " x[1,6] ≥ 3000\n",
      " x[1,5] ≥ 2800\n",
      " x[i,j] ≥ 0, integer, ∀ i ∈ {1}, j ∈ {1,2,…,10,11}\n",
      "la respuesta es6.86287176e6\n"
     ]
    }
   ],
   "source": [
    "using JuMP,Cbc\n",
    "\n",
    "coef=[110 210  60.5  53.5  143.25   155.25  136   66.25   33.75  22  26.62 ]\n",
    "moda=Model(solver=CbcSolver())\n",
    "@variable(moda,x[1:1,1:11]>=0,Int)\n",
    "@objective(moda,Max,sum(coef.*x))\n",
    "\n",
    "\n",
    "# Restricciones de materia prima\n",
    "\n",
    "@constraint(moda,3x[1,1]+2.5x[1,6]<=45000)\n",
    "@constraint(moda,2x[1,1]+1.5x[1,5]+1.5x[1,6]+2x[1,7]<=28000)\n",
    "@constraint(moda,1.5x[1,2]<=9000)\n",
    "@constraint(moda,1.5x[1,3]+0.5x[4]<=18000)\n",
    "@constraint(moda,2x[5]+1.5x[1,11]<=30000)\n",
    "@constraint(moda,3x[7]+1.5x[1,10]<=20000)\n",
    "@constraint(moda,1.5x[1,8]+0.5x[1,9]<=30000)\n",
    "\n",
    "#Restricciones de demanda\n",
    "@constraint(moda,x[1,7]<=5500)\n",
    "@constraint(moda,x[1,10]<=6000)\n",
    "@constraint(moda,x[1,2]<=4000)\n",
    "@constraint(moda,x[1,3]<=12000)\n",
    "@constraint(moda,x[1,4]<=15000)\n",
    "@constraint(moda,x[1,1]<=7000)\n",
    "@constraint(moda,x[1,1]>=4200)\n",
    "@constraint(moda,x[1,6]<=5000)\n",
    "@constraint(moda,x[1,6]>=3000)\n",
    "@constraint(moda,x[1,5]>=2800)\n",
    "\n",
    "\n",
    "\n",
    "print(moda)\n",
    "\n",
    "status=solve(moda)\n",
    "println(\"la respuesta es\",getobjectivevalue(moda))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cuarto caso"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso4-1.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso4-1.PNG?raw=true)\n",
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso4-2.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso4-2.PNG?raw=true)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quinto caso"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso5-1.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso5-1.PNG?raw=true)\n",
    "\n",
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso5-2.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso5-2.PNG?raw=true)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Min 300 e1[1,1] + 10000 e1[2,1] + 600 e1[3,1] + 200 e1[4,1] + 500 e1[6,1] + 300 e1[1,2] + 10000 e1[2,2] + 600 e1[3,2] + 200 e1[4,2] + 500 e1[6,2] + 300 e1[1,3] + 10000 e1[2,3] + 600 e1[3,3] + 200 e1[4,3] + 500 e1[6,3] + 400 e2[2,1] + 300 e2[3,1] + 500 e2[4,1] + 10000 e2[5,1] + 300 e2[6,1] + 400 e2[2,2] + 300 e2[3,2] + 500 e2[4,2] + 10000 e2[5,2] + 300 e2[6,2] + 400 e2[2,3] + 300 e2[3,3] + 500 e2[4,3] + 10000 e2[5,3] + 300 e2[6,3] + 700 e3[1,1] + 500 e3[2,1] + 200 e3[3,1] + 10000 e3[4,1] + 400 e3[5,1] + 700 e3[1,2] + 500 e3[2,2] + 200 e3[3,2] + 10000 e3[4,2] + 400 e3[5,2] + 700 e3[1,3] + 500 e3[2,3] + 200 e3[3,3] + 10000 e3[4,3] + 400 e3[5,3]\n",
      "Subject to\n",
      " e1[1,1] + e1[2,1] + e1[3,1] + e1[4,1] + e1[5,1] + e1[6,1] ≥ 270\n",
      " e1[1,1] + e1[2,1] + e1[3,1] + e1[4,1] + e1[5,1] + e1[6,1] ≤ 324\n",
      " e2[1,1] + e2[2,1] + e2[3,1] + e2[4,1] + e2[5,1] + e2[6,1] ≥ 330\n",
      " e2[1,1] + e2[2,1] + e2[3,1] + e2[4,1] + e2[5,1] + e2[6,1] ≤ 396\n",
      " e3[1,1] + e3[2,1] + e3[3,1] + e3[4,1] + e3[5,1] + e3[6,1] ≥ 300\n",
      " e3[1,1] + e3[2,1] + e3[3,1] + e3[4,1] + e3[5,1] + e3[6,1] ≤ 360\n",
      " e1[1,2] + e1[2,2] + e1[3,2] + e1[4,2] + e1[5,2] + e1[6,2] ≥ 270\n",
      " e1[1,2] + e1[2,2] + e1[3,2] + e1[4,2] + e1[5,2] + e1[6,2] ≤ 324\n",
      " e2[1,2] + e2[2,2] + e2[3,2] + e2[4,2] + e2[5,2] + e2[6,2] ≥ 330\n",
      " e2[1,2] + e2[2,2] + e2[3,2] + e2[4,2] + e2[5,2] + e2[6,2] ≤ 396\n",
      " e3[1,2] + e3[2,2] + e3[3,2] + e3[4,2] + e3[5,2] + e3[6,2] ≥ 300\n",
      " e3[1,2] + e3[2,2] + e3[3,2] + e3[4,2] + e3[5,2] + e3[6,2] ≤ 360\n",
      " e1[1,3] + e1[2,3] + e1[3,3] + e1[4,3] + e1[5,3] + e1[6,3] ≥ 270\n",
      " e1[1,3] + e1[2,3] + e1[3,3] + e1[4,3] + e1[5,3] + e1[6,3] ≤ 324\n",
      " e2[1,3] + e2[2,3] + e2[3,3] + e2[4,3] + e2[5,3] + e2[6,3] ≥ 330\n",
      " e2[1,3] + e2[2,3] + e2[3,3] + e2[4,3] + e2[5,3] + e2[6,3] ≤ 396\n",
      " e3[1,3] + e3[2,3] + e3[3,3] + e3[4,3] + e3[5,3] + e3[6,3] ≥ 300\n",
      " e3[1,3] + e3[2,3] + e3[3,3] + e3[4,3] + e3[5,3] + e3[6,3] ≤ 360\n",
      " e1[1,1] + e2[1,1] + e3[1,1] = 144\n",
      " e1[1,2] + e2[1,2] + e3[1,2] = 171\n",
      " e1[1,3] + e2[1,3] + e3[1,3] = 135\n",
      " e1[2,1] + e2[2,1] + e3[2,1] = 222\n",
      " e1[2,2] + e2[2,2] + e3[2,2] = 168.00000000000003\n",
      " e1[2,3] + e2[2,3] + e3[2,3] = 210\n",
      " e1[3,1] + e2[3,1] + e3[3,1] = 165\n",
      " e1[3,2] + e2[3,2] + e3[3,2] = 176\n",
      " e1[3,3] + e2[3,3] + e3[3,3] = 209\n",
      " e1[4,1] + e2[4,1] + e3[4,1] = 98.00000000000001\n",
      " e1[4,2] + e2[4,2] + e3[4,2] = 140\n",
      " e1[4,3] + e2[4,3] + e3[4,3] = 112\n",
      " e1[5,1] + e2[5,1] + e3[5,1] = 195\n",
      " e1[5,2] + e2[5,2] + e3[5,2] = 170\n",
      " e1[5,3] + e2[5,3] + e3[5,3] = 135\n",
      " e1[6,1] + e2[6,1] + e3[6,1] = 153\n",
      " e1[6,2] + e2[6,2] + e3[6,2] = 126.00000000000001\n",
      " e1[6,3] + e2[6,3] + e3[6,3] = 171\n",
      " e1[i,j] ≥ 0 ∀ i ∈ {1,2,…,5,6}, j ∈ {1,2,3}\n",
      " e2[i,j] ≥ 0 ∀ i ∈ {1,2,…,5,6}, j ∈ {1,2,3}\n",
      " e3[i,j] ≥ 0 ∀ i ∈ {1,2,…,5,6}, j ∈ {1,2,3}\n",
      "el minimo costo es=428900.0\n",
      "la escuela 1 =6×3 DataFrames.DataFrame\n",
      "│ Row │ x1    │ x2    │ x3    │\n",
      "├─────┼───────┼───────┼───────┤\n",
      "│ 1   │ 0.0   │ 0.0   │ 3.0   │\n",
      "│ 2   │ 0.0   │ 0.0   │ 0.0   │\n",
      "│ 3   │ 0.0   │ 0.0   │ 20.0  │\n",
      "│ 4   │ 98.0  │ 140.0 │ 112.0 │\n",
      "│ 5   │ 195.0 │ 170.0 │ 135.0 │\n",
      "│ 6   │ 0.0   │ 0.0   │ 0.0   │\n",
      "la escuela 2 =6×3 DataFrames.DataFrame\n",
      "│ Row │ x1    │ x2    │ x3    │\n",
      "├─────┼───────┼───────┼───────┤\n",
      "│ 1   │ 144.0 │ 171.0 │ 132.0 │\n",
      "│ 2   │ 222.0 │ 168.0 │ 210.0 │\n",
      "│ 3   │ 0.0   │ 0.0   │ 0.0   │\n",
      "│ 4   │ 0.0   │ 0.0   │ 0.0   │\n",
      "│ 5   │ 0.0   │ 0.0   │ 0.0   │\n",
      "│ 6   │ 0.0   │ 0.0   │ 0.0   │\n",
      "la escuela 3 =6×3 DataFrames.DataFrame\n",
      "│ Row │ x1    │ x2    │ x3    │\n",
      "├─────┼───────┼───────┼───────┤\n",
      "│ 1   │ 0.0   │ 0.0   │ 0.0   │\n",
      "│ 2   │ 0.0   │ 0.0   │ 0.0   │\n",
      "│ 3   │ 165.0 │ 176.0 │ 189.0 │\n",
      "│ 4   │ 0.0   │ 0.0   │ 0.0   │\n",
      "│ 5   │ 0.0   │ 0.0   │ 0.0   │\n",
      "│ 6   │ 153.0 │ 126.0 │ 171.0 │\n"
     ]
    }
   ],
   "source": [
    "using JuMP,Cbc,DataFrames\n",
    "c1=[300 300 300;\n",
    "    10000 10000 10000;\n",
    "    600 600 600;\n",
    "    200 200 200;\n",
    "    0 0 0;\n",
    "    500 500 500]\n",
    "c2=[0 0 0;\n",
    "    400 400 400;\n",
    "    300 300 300;\n",
    "    500 500 500;\n",
    "    10000 10000 10000;\n",
    "    300 300 300]\n",
    "c3=[700 700 700;\n",
    "    500 500 500;\n",
    "    200 200 200;\n",
    "    10000 10000 10000;\n",
    "    400 400 400;\n",
    "    0 0 0]\n",
    "\n",
    "nuea=[450;600;550;350;500;450]# numero de estudiantes por area\n",
    "p6=[0.32;0.37;0.30;0.28;0.39;0.34]\n",
    "p7=[0.38;0.28;0.32;0.40;0.34;0.28]\n",
    "p8=[0.30;0.35;0.38;0.32;0.27;0.38]\n",
    "\n",
    "esexto=nuea.*p6\n",
    "eseptimo=nuea.*p7\n",
    "eoctavo=nuea.*p8\n",
    "capacidad=[900 1100 1000]\n",
    "estotal=[esexto eseptimo eoctavo]\n",
    "\n",
    "m=Model(solver=CbcSolver())\n",
    "@variables m begin\n",
    "    e1[1:6,1:3]>=0\n",
    "    e2[1:6,1:3]>=0\n",
    "    e3[1:6,1:3]>=0\n",
    "end\n",
    "@objective(m,Min,sum(e1.*c1)+sum(e2.*c2)+sum(e3.*c3))\n",
    "\n",
    "for j=1:3\n",
    "\n",
    "@constraint(m,sum(e1[i,j] for i=1:6)>=0.3*capacidad[1])    \n",
    "@constraint(m,sum(e1[i,j] for i=1:6)<=0.36*capacidad[1]) \n",
    "@constraint(m,sum(e2[i,j] for i=1:6)>=0.3*capacidad[2])    \n",
    "@constraint(m,sum(e2[i,j] for i=1:6)<=0.36*capacidad[2])    \n",
    "@constraint(m,sum(e3[i,j] for i=1:6)>=0.3*capacidad[3])    \n",
    "@constraint(m,sum(e3[i,j] for i=1:6)<=0.36*capacidad[3])    \n",
    "    \n",
    "end\n",
    "\n",
    "for i=1:6\n",
    "    @constraint(m,e1[i,1]+e2[i,1]+e3[i,1]==esexto[i])\n",
    "    @constraint(m,e1[i,2]+e2[i,2]+e3[i,2]==eseptimo[i])\n",
    "    @constraint(m,e1[i,3]+e2[i,3]+e3[i,3]==eoctavo[i])\n",
    "end\n",
    "print(m)\n",
    "status=solve(m)\n",
    "\n",
    "println(\"el minimo costo es=\",getobjectivevalue(m))\n",
    "   println(\"la escuela 1 =\",DataFrame(getvalue(e1)))\n",
    "   println(\"la escuela 2 =\",DataFrame(getvalue(e2)))\n",
    "println(\"la escuela 3 =\",DataFrame(getvalue(e3)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso6-1.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso6-1.PNG?raw=true)\n",
    "\n",
    "\n",
    "\n",
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso6-2.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso6-2.PNG?raw=true)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso7-1.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso7-1.PNG?raw=true)\n",
    "\n",
    "[<img src=\"https://github.com/jorgeiv500/diplomado/blob/master/Caso7-2.PNG?raw=true\" height=\"700\" width=\"700\">](https://github.com/jorgeiv500/diplomado/blob/master/Caso7-2.PNG?raw=true)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Max 100 x[1,1] + 400 x[2,1] + 200 x[3,1] + 200 x[4,1] + 100 x[5,1] + 200 x[2,2] + 800 x[3,2] + 100 x[1,3] + 100 x[2,3] + 100 x[3,3] + 100 x[4,3] + 600 x[5,3] + 267 x[1,4] + 153 x[2,4] + 99 x[3,4] + 451 x[4,4] + 30 x[5,4]\n",
      "Subject to\n",
      " x[1,1] + x[1,2] + x[1,3] + x[1,4] + x[1,5] = 1\n",
      " x[2,1] + x[2,2] + x[2,3] + x[2,4] + x[2,5] = 1\n",
      " x[3,1] + x[3,2] + x[3,3] + x[3,4] + x[3,5] = 1\n",
      " x[4,1] + x[4,2] + x[4,3] + x[4,4] + x[4,5] = 1\n",
      " x[5,1] + x[5,2] + x[5,3] + x[5,4] + x[5,5] = 1\n",
      " x[1,1] + x[2,1] + x[3,1] + x[4,1] + x[5,1] = 1\n",
      " x[1,2] + x[2,2] + x[3,2] + x[4,2] + x[5,2] = 1\n",
      " x[1,3] + x[2,3] + x[3,3] + x[4,3] + x[5,3] = 1\n",
      " x[1,4] + x[2,4] + x[3,4] + x[4,4] + x[5,4] = 1\n",
      " x[1,5] + x[2,5] + x[3,5] + x[4,5] + x[5,5] = 1\n",
      " x[i,j] ≥ 0 ∀ i ∈ {1,2,3,4,5}, j ∈ {1,2,3,4,5}\n",
      "el maximo es=2251.0\n",
      "las asignaciones son=5×5 DataFrames.DataFrame\n",
      "│ Row │ x1  │ x2  │ x3  │ x4  │ x5  │\n",
      "├─────┼─────┼─────┼─────┼─────┼─────┤\n",
      "│ 1   │ 0.0 │ 0.0 │ 0.0 │ 0.0 │ 1.0 │\n",
      "│ 2   │ 1.0 │ 0.0 │ 0.0 │ 0.0 │ 0.0 │\n",
      "│ 3   │ 0.0 │ 1.0 │ 0.0 │ 0.0 │ 0.0 │\n",
      "│ 4   │ 0.0 │ 0.0 │ 0.0 │ 1.0 │ 0.0 │\n",
      "│ 5   │ 0.0 │ 0.0 │ 1.0 │ 0.0 │ 0.0 │\n"
     ]
    }
   ],
   "source": [
    "using  JuMP, Cbc,DataFrames\n",
    "\n",
    "proyectos=Model(solver=CbcSolver())\n",
    "Pij=[100 0 100 267 0;\n",
    "    400 200 100 153 0;\n",
    "    200 800 100 99 0;\n",
    "    200 0 100 451 0;\n",
    "    100 0 600 30 0]\n",
    "@variable(proyectos, x[1:5,1:5]>=0 )\n",
    "@objective(proyectos,Max,sum(Pij.*x))\n",
    "\n",
    "for i=1:5 #suna de las filas\n",
    "    @constraint(proyectos, sum(x[i,j] for j=1:5)==1)\n",
    "end\n",
    "\n",
    "for j=1:5 #sumatoria de las columnas\n",
    "    @constraint(proyectos, sum(x[i,j] for i=1:5)==1)\n",
    "end\n",
    "print(proyectos)\n",
    "status=solve(proyectos)\n",
    "println(\"el maximo es=\",getobjectivevalue(proyectos))\n",
    "println(\"las asignaciones son=\",DataFrame(getvalue(x)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.0",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": true,
   "user_envs_cfg": true
  },
  "nbTranslate": {
   "displayLangs": [
    "*"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "fr",
   "useGoogleTranslate": true
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "12px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false,
   "widenNotebook": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
